home *** CD-ROM | disk | FTP | other *** search
/ Exame Informatica 142 / Exame Informatica 142.iso / Programas / Wiki / WikidPad-1.9beta2.exe / {app} / extensions / PloticusClBridge.py < prev    next >
Encoding:
Python Source  |  2006-12-11  |  7.5 KB  |  212 lines

  1. import os, os.path
  2. from subprocess import list2cmdline
  3.  
  4. import wx
  5.  
  6. from pwiki.TempFileSet import createTempFile
  7. from pwiki.StringOps import mbcsEnc, mbcsDec, lineendToOs
  8.  
  9. WIKIDPAD_PLUGIN = (("InsertionByKey", 1), ("Options", 1))
  10.  
  11. def describeInsertionKeys(ver, app):
  12.     """
  13.     API function for "InsertionByKey" plugins
  14.     Returns a sequence of tuples describing the supported
  15.     insertion keys. Each tuple has the form (insKey, exportTypes, handlerFactory)
  16.     where insKey is the insertion key handled, exportTypes is a sequence of
  17.     strings describing the supported export types and handlerFactory is
  18.     a factory function (normally a class) taking the wxApp object as
  19.     parameter and returning a handler object fulfilling the protocol
  20.     for "insertion by key" (see EqnHandler as example).
  21.  
  22.     ver -- API version (can only be 1 currently)
  23.     app -- wxApp object
  24.     """
  25.     return (
  26.             (u"ploticus", ("html_single", "html_previewWX", "html_preview", "html_multi"), PltHandler),
  27.             )
  28.  
  29.  
  30. class PltHandler:
  31.     """
  32.     Base class fulfilling the "insertion by key" protocol.
  33.     """
  34.     def __init__(self, app):
  35.         self.app = app
  36.         self.extAppExe = None
  37.         
  38.     def taskStart(self, exporter, exportType):
  39.         """
  40.         This is called before any call to createContent() during an
  41.         export task.
  42.         An export task can be a single HTML page for
  43.         preview or a single page or a set of pages for export.
  44.         exporter -- Exporter object calling the handler
  45.         exportType -- string describing the export type
  46.         
  47.         Calls to createContent() will only happen after a 
  48.         call to taskStart() and before the call to taskEnd()
  49.         """
  50.         # Find Ploticus executable by configuration setting
  51.         self.extAppExe = self.app.getGlobalConfig().get("main",
  52.                 "plugin_ploticus_exePath", "")
  53.         
  54.     def taskEnd(self):
  55.         """
  56.         Called after export task ended and after the last call to
  57.         createContent().
  58.         """
  59.         pass
  60.  
  61.  
  62.     def createContent(self, exporter, exportType, insToken):
  63.         """
  64.         Handle an insertion and create the appropriate content.
  65.  
  66.         exporter -- Exporter object calling the handler
  67.         exportType -- string describing the export type
  68.         insToken -- insertion token to create content for (see also 
  69.                 PageAst.Insertion)
  70.  
  71.         An insertion token has the following member variables:
  72.             key: insertion key (unistring)
  73.             value: value of an insertion (unistring)
  74.             appendices: sequence of strings with the appendices
  75.  
  76.         Meaning and type of return value is solely defined by the type
  77.         of the calling exporter.
  78.         
  79.         For HtmlXmlExporter a unistring is returned with the HTML code
  80.         to insert instead of the insertion.        
  81.         """
  82.         # Retrieve quoted content of the insertion
  83.         bstr = lineendToOs(mbcsEnc(insToken.value, "replace")[0])
  84.  
  85.         if not bstr:
  86.             # Nothing in, nothing out
  87.             return u""
  88.         
  89.         if self.extAppExe == "":
  90.             # No path to MimeTeX executable -> show message
  91.             return "<pre>[Please set path to Ploticus executable]</pre>"
  92.  
  93.         # Get exporters temporary file set (manages creation and deletion of
  94.         # temporary files)
  95.         tfs = exporter.getTempFileSet()
  96.  
  97.         pythonUrl = (exportType != "html_previewWX")
  98.         dstFullPath = tfs.createTempFile("", ".gif", relativeTo="")
  99.         url = tfs.getRelativeUrl(None, dstFullPath, pythonUrl=pythonUrl)
  100.         
  101.         baseDir = os.path.dirname(exporter.getMainControl().getWikiConfigPath())
  102.  
  103.         # Store token content in a temporary file
  104.         srcfilepath = createTempFile(bstr, ".plt")
  105.         try:
  106.             cmdline = list2cmdline((self.extAppExe, "-dir", baseDir,
  107.                     srcfilepath, "-gif", "-o", dstFullPath))
  108.  
  109.             # Run external application
  110.             childIn, childOut, childErr = os.popen3(cmdline, "b")
  111.             
  112.             if u"noerror" in [a.strip() for a in insToken.appendices]:
  113.                 childErr.read()
  114.                 errResponse = ""
  115.             else:
  116.                 errResponse = childErr.read()
  117.         finally:
  118.             os.unlink(srcfilepath)
  119.             
  120.         if errResponse != "":
  121.             errResponse = mbcsDec(errResponse, "replace")[0]
  122.             return u"<pre>[Ploticus error: %s]</pre>" % errResponse
  123.  
  124.         # Return appropriate HTML code for the image
  125.         if exportType == "html_previewWX":
  126.             # Workaround for internal HTML renderer
  127.             return u'<img src="%s" border="0" align="bottom" /> ' % url
  128.         else:
  129.             return u'<img src="%s" border="0" align="bottom" />' % url
  130.  
  131.  
  132.     def getExtraFeatures(self):
  133.         """
  134.         Returns a list of bytestrings describing additional features supported
  135.         by the plugin. Currently not specified further.
  136.         """
  137.         return ()
  138.         
  139.  
  140. def registerOptions(ver, app):
  141.     """
  142.     API function for "Options" plugins
  143.     Register configuration options and their GUI presentation
  144.     ver -- API version (can only be 1 currently)
  145.     app -- wxApp object
  146.     """
  147.     # Register option
  148.     app.getDefaultGlobalConfigDict()[("main", "plugin_ploticus_exePath")] = u""
  149.     # Register panel in options dialog
  150.     app.addOptionsDlgPanel(PloticusOptionsPanel, u"  Ploticus")
  151.  
  152.  
  153. class PloticusOptionsPanel(wx.Panel):
  154.     def __init__(self, parent, optionsDlg, app):
  155.         """
  156.         Called when "Options" dialog is opened to show the panel.
  157.         Transfer here all options from the configuration file into the
  158.         text fields, check boxes, ...
  159.         """
  160.         wx.Panel.__init__(self, parent)
  161.         self.app = app
  162.         
  163.         pt = self.app.getGlobalConfig().get("main", "plugin_ploticus_exePath", "")
  164.         
  165.         self.tfPath = wx.TextCtrl(self, -1, pt)
  166.  
  167.         mainsizer = wx.BoxSizer(wx.VERTICAL)
  168.  
  169.         inputsizer = wx.BoxSizer(wx.HORIZONTAL)
  170.         inputsizer.Add(wx.StaticText(self, -1, "Path to Ploticus:"), 0,
  171.                 wx.ALL | wx.EXPAND, 5)
  172.         inputsizer.Add(self.tfPath, 1, wx.ALL | wx.EXPAND, 5)
  173.         mainsizer.Add(inputsizer, 0, wx.EXPAND)
  174.         
  175.         self.SetSizer(mainsizer)
  176.         self.Fit()
  177.  
  178.     def setVisible(self, vis):
  179.         """
  180.         Called when panel is shown or hidden. The actual wxWindow.Show()
  181.         function is called automatically.
  182.         
  183.         If a panel is visible and becomes invisible because another panel is
  184.         selected, the plugin can veto by returning False.
  185.         When becoming visible, the return value is ignored.
  186.         """
  187.         return True
  188.  
  189.     def checkOk(self):
  190.         """
  191.         Called when "OK" is pressed in dialog. The plugin should check here if
  192.         all input values are valid. If not, it should return False, then the
  193.         Options dialog automatically shows this panel.
  194.         
  195.         There should be a visual indication about what is wrong (e.g. red
  196.         background in text field). Be sure to reset the visual indication
  197.         if field is valid again.
  198.         """
  199.         return True
  200.  
  201.     def handleOk(self):
  202.         """
  203.         This is called if checkOk() returned True for all panels. Transfer here
  204.         all values from text fields, checkboxes, ... into the configuration
  205.         file.
  206.         """
  207.         pt = self.tfPath.GetValue()
  208.         
  209.         self.app.getGlobalConfig().set("main", "plugin_ploticus_exePath", pt)
  210.  
  211.  
  212.